package com.yiwei.ydd.view;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.UI;
import com.yiwei.ydd.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by xsooy-pc on 2017/4/24.
 */

public class InputMoneyDialog extends Dialog {


    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.edit_content)
    EditText editContent;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Context context;


    public InputMoneyDialog(Context context) {
        this(context, R.style.DialogStyle);
    }

    public InputMoneyDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_input_money);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = Util.getDisplayMetrics(context).widthPixels;
        mWindow.setAttributes(lp);


    }


    private DialogDefaultClickListener listener;

    public InputMoneyDialog setDialogClickListener(DialogDefaultClickListener listener) {
        this.listener = listener;
        return this;
    }

    @OnClick({R.id.btn_close, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                dismiss();
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editContent))) {
                    ToastUtil.makeText(context, "金额不能为空");
                    return;
                }

                if (UI.toInteger(editContent) % 100 != 0) {
                    ToastUtil.makeText(context, "必须为100的倍数");
                    return;
                }
                if (listener != null) {
                    listener.confirm(UI.toString(editContent));
                }
                dismiss();
                break;
        }
    }


    public interface DialogDefaultClickListener {
        public abstract void confirm(String money);


    }
}
